package com.funmi.lizip.entity;

/*import com.baidu.ai.aip.utils.Base64Util;
import com.baidu.ai.aip.utils.FileUtil;
import com.baidu.ai.aip.utils.HttpUtil;*/

import cn.hutool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.funmi.lizip.common.base.Base64Util;
import com.funmi.lizip.common.base.FileUtil;
import com.funmi.lizip.common.base.HttpUtil;
import com.funmi.lizip.common.base.HttpUtils;


import java.net.URLEncoder;
import java.util.HashMap;
import java.util.UUID;


/**
 * 动物识别
 */
public class Animal {

    /**
     * FileUtil,Base64Util,HttpUtil,GsonUtils请从
     * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
     * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
     * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
     * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
     * 重要提示代码中所需工具类
     * 下载
     */
    public static String animal() {



        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/animal";
        try {
            // 本地文件路径
            // String filePath = "D:/jiandujiandu.jpg";   // 下载的路径 具体到图片,名字


            String fileUrl = "https://ts1.cn.mm.bing.net/th/id/R-C.aad355c895a4aeffe9e5f2ec2bd7a740?rik=g4KOLr1G9yu%2fOg&riu=http%3a%2f%2fimg.pconline.com.cn%2fimages%2fupload%2fupc%2ftx%2fphotoblog%2f1210%2f25%2fc3%2f14650487_14650487_1351140058714_mthumb.jpg&ehk=r6XA6w2E9ZQE4YlrlG2AC%2biunsZI9%2b9DcPhZ3UTlnmo%3d&risl=&pid=ImgRaw&r=0";   // 下载的路径
            String filePath = "D:/"; // 下载的路径
            String fileName = UUID.randomUUID().toString() + ".jpg";  // 具体到名字,图片
            HttpUtils.download(fileUrl, filePath, fileName);


            //byte[] imgData = FileUtil.readFileByBytes(filePath);
            byte[] imgData = FileUtil.readFileByBytes(filePath + fileName);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");

            String param = "image=" + imgParam;

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = "24.ac2681c4f183ac86c0df7babfe7e3d65.2592000.1670984179.282335-28294342";  // AuthService生成的token值

            String result = HttpUtil.post(url, accessToken, param);
            // 拿取第一个JSON对象
            HashMap map = JSON.parseObject(result, HashMap.class);
            Object result1 = map.get("result");
            JSONArray jsonArray = JSON.parseArray(result1.toString());
            JSONObject jsonFirst = jsonArray.getJSONObject(0);
            System.out.println(jsonFirst);

            // System.out.println(result);
            return jsonFirst.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        Animal.animal();
    }
}
